﻿using System;

// 定义抽象基类
public abstract class Shape
{
    public abstract double CalculateArea();
    public abstract bool IsValid();
}

// 长方形类
public class Rectangle : Shape
{
    public double Length { get; set; }
    public double Width { get; set; }

    public Rectangle(double length, double width)
    {
        Length = length;
        Width = width;
    }

    public override double CalculateArea()
    {
        return IsValid() ? Length * Width : 0;
    }

    public override bool IsValid()
    {
        return Length > 0 && Width > 0;
    }
}

// 正方形类
public class Square : Shape
{
    public double Side { get; set; }

    public Square(double side)
    {
        Side = side;
    }

    public override double CalculateArea()
    {
        return IsValid() ? Side * Side : 0;
    }

    public override bool IsValid()
    {
        return Side > 0;
    }
}

// 三角形类
public class Triangle : Shape
{
    public double A { get; set; }
    public double B { get; set; }
    public double C { get; set; }

    public Triangle(double a, double b, double c)
    {
        A = a;
        B = b;
        C = c;
    }

    public override double CalculateArea()
    {
        if (!IsValid()) return 0;

        double s = (A + B + C) / 2;
        return Math.Sqrt(s * (s - A) * (s - B) * (s - C));
    }

    public override bool IsValid()
    {
        return A > 0 && B > 0 && C > 0 &&
               A + B > C &&
               A + C > B &&
               B + C > A;
    }
}

class Program
{
    static void Main()
    {
        // 测试长方形
        var rect = new Rectangle(5, 3);
        Console.WriteLine($"长方形面积: {rect.CalculateArea()}, 有效: {rect.IsValid()}");

        // 测试正方形
        var square = new Square(4);
        Console.WriteLine($"正方形面积: {square.CalculateArea()}, 有效: {square.IsValid()}");

        // 测试三角形
        var triangle = new Triangle(3, 4, 5);
        Console.WriteLine($"三角形面积: {triangle.CalculateArea():F2}, 有效: {triangle.IsValid()}");
    }
}